import cv2, time, random, os, datetime
import os, sys, pyautogui, traceback
import numpy as np
import mss
import action2
import random

# from datetime import datetime

# 检测系统
print('操作系统:', sys.platform)
if sys.platform == 'darwin':
    scalar = True
else:
    scalar = False

# 读取文件 精度控制   显示名字
imgs = action2.load_imgs()
# pyautogui.PAUSE = 0.05
pyautogui.FAILSAFE = False

start_time = time.time()
# print('程序启动，现在时间', time.ctime())

# 截屏，并裁剪以加速
upleft = (0, 0)
if scalar == True:
    downright = (1136, 700)
else:
    downright = (1900, 1080)
a, b = upleft
c, d = downright
monitor = {"top": b, "left": a, "width": c, "height": d}
# print(monitor)
start = time.time()
accounts = {'account_1_qiang': ['1_1_piaoliu', '1_2_qingshan', '1_3_wuming', '1_4_linyuan']}
actions = ['takeGouYu', 'takeJiaCheng', 'takeTili', 'yinyangliao', 'fengmo', 'qiandao', 'email']

# constants
last_click = None


# 以上启动，载入设置
##########################################################
def select_mode():
    global start
    end = time.time()
    hours, rem = divmod(end - start, 3600)
    minutes, seconds = divmod(rem, 60)
    print("运行时间：{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), seconds))
    print(datetime.datetime.now())

    print('''\n菜单：  鼠标移动到最右侧中止并返回菜单页面，0退出
        1 结界突破
        2 御魂(司机)
        3 御魂(打手)
        4 御魂(单刷)
        5 探索(司机)
        6 探索(打手)
        7 探索(单刷)
        8 百鬼夜行
        9 自动斗技
        10 当前活动（超鬼王）
        11 结界自动合卡（太阴和伞室内）
        12 厕纸抽卡
        13 蓝蛋升级
        14 秘境召唤
        15 妖气封印和秘闻
        16 换号
        ''')
    # action.alarm(1)
    raw = input("选择功能模式：")
    try:
        index = int(raw)
    except:
        print('请输入数字')
        select_mode()

    mode = [0, huanhao]
    try:
        command = mode[index]
    except:
        print('数字超出范围')
        select_mode()

    if index == 0:
        quit()
    else:
        start = time.time()
        command()


def click(img, sec=0, break_count=15):
    for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]:
        screen = action2.screenshot(monitor)
        want = imgs[img]
        pts = action2.locate(screen, want, 0)
        if not len(pts) == 0:
            size = want[0].shape
            h, w, ___ = size
            xx = action2.cheat(pts[0], w, h)
            time.sleep(.5)
            pyautogui.click(xx)
            t = random.randint(1, 2)
            t2 = random.randint(140, 160) / 100
            time.sleep(t2 + sec)
            return True
        else:
            if i == break_count:
                print("没有找到" + img)
                return False
            time.sleep(.5)


def click2(img, ww=0, hh=0):
    pyautogui.click([ww, hh])
    select_mode()


########################################################
def fengmo():
    click("fengmo_1_start")
    click("fengmo_2_douji")
    click("fengmo_3_canjia")
    click("fengmo_4_qianwang")
    click("fengmo_5_dianji")
    t = random.randint(2, 3)
    time.sleep(t)
    click("fengmo_5_dianji")
    t = random.randint(2, 3)
    time.sleep(t)
    click("fengmo_5_dianji")
    t = random.randint(2, 3)
    time.sleep(t)
    click("fengmo_5_dianji")
    t = random.randint(2, 3)
    time.sleep(t)
    for i in [1, 2, 3]:
        click("fengmo_5_dianji")
        if i == random.randint(1, 3):
            break
    click("fengmo_6_jiangli")
    click("fengmo_7_lingqujianglichenggong")

    for i in [1, 2, 3]:
        click("fengmo_8_xunzhaoshouling")
        dayOfWeek = datetime.datetime.now().isoweekday()
        print(dayOfWeek)
        click("fengmo_9_guaishou" + str(dayOfWeek))
        click("fengmo_10_jijietiaozhan")
        click("fengmo_11_jijiequeding")
        t = random.randint(2, 3)
        time.sleep(t)
        meiyouzhaodao = click("fengmo_11_1_meiyouzhaodaoguaishou")
        if (meiyouzhaodao == False):
            break

    click("fengmo_11_jijiequeding")
    t = random.randint(400, 420)
    time.sleep(t)

    click("fengmo_12_1_shengli")
    click("fengmo_12_chenggong")
    click("fengmo_13_fanhui")
    click("fengmo_13_chenggong")
    click("fengmo_14_chenggong")
    click("fengmo_15_fanhui")
    click("fengmo_16_quedingtuicu")
    click("fengmo_18_fanhui")


# 领取体力
def yyl_tili():
    click("yyl_tili_1")  # 点击体力
    click("yyl_tili_2_take")  # 取出体力
    click("yyl_tili_3_close")  # 关闭蒙板
    last = click("yyl_tili_4_close")  # 关闭领取体力
    if last:
        print("领取体力成功")
    else:
        print("！！！！领取体力失败！！！！")


# 领取勾玉
def yyl_gouyu():
    last = click("yyl_gouyu_1")
    if last:
        print("领取勾玉成功")
    else:
        print("！！！！领取勾玉失败！！！！")


# 放结界卡
def yyl_put_card():
    click("yyl_put_card_1_begin")  # 开始放结界卡
    click("yyl_put_card_2_all")  # 点击全部
    click("yyl_put_card_3_select_all_taigu")  # 选择所有太鼓
    click("yyl_put_card_4_select_one_taigu")  # 选择其中一个太鼓
    click("yyl_put_card_5_active_taigu")  # 激活太鼓
    last = click("yyl_put_card_6_end")  # 关闭
    if last:
        print("领取勾玉成功")
    else:
        print("！！！！领取勾玉失败！！！！")


# 寄养
def yyl_jiyang():
    for i in [1, 2, 3, 4, 5, 6]:
        if click("yyl_jiyang_1_begin"):  # 寄养开始
            break
        else:
            continue
    click("yyl_jiyang_2_entry")  # 进入寄养
    success = click("yyl_jiyang_3_select_gouyu")  # 选择勾玉
    if not success:
        success = click("yyl_jiyang_4_select_tili")  # 选择体力
    if not success:
        success = click("yyl_jiyang_5_select_jinbi")  # 选择金币
    if success:
        click("yyl_jiyang_6_entry_jiejie")
        click("yinyangliao_12_4_fangshishen")
        click("yinyangliao_12_5_fangshishenjieshu")
        click("yinyangliao_12_6_fanhui")
        click("yinyangliao_12_7_fanhui")
        click("yinyangliao_12_8_fanhui")
        click("yinyangliao_16_fanhui")

    else:
        click("yinyangliao_13_fail_fanhui")
        click("yinyangliao_14_fanhui")
        click("yinyangliao_15_fanhui", 2)
        click("yinyangliao_16_fanhui")

        print(2)
    click("yinyangliao_16_fanhui")


def yinyangliao():
    yyl_tili()
    yyl_gouyu()
    yyl_put_card()
    # click("yinyangliao_1_zhankai",2)
    # click("yinyangliao_2_jinru")
    # click("yinyangliao_3_jinrujiejie")
    # #领取体力
    # click("yinyangliao_4_lingqutili")
    # click("yinyangliao_5_quchutili")
    # click("yinyangliao_6_lingquchenggong")
    # click("yinyangliao_8_guanbitili")
    # #领取勾玉
    # click("yinyangliao_9_lingqugouyu")
    # 放结界卡
    click("yinyangliao_17_fangjiejieka")
    click("yinyangliao_18_fangjiejieka_quanbu")
    click("yinyangliao_19_fangjiejieka_gouyu")
    click("yinyangliao_20_gouyujiejieka")
    click("yinyangliao_21_jihuo")
    click("yinyangliao_22_tuichu")
    # 寄养
    for i in [1, 2, 3, 4, 5, 6]:
        if click("yinyangliao_10_jiya"):
            break
        else:
            continue
    click("yinyangliao_11_jiyang")
    success = click("yinyangliao_12_jiyanggouyu")
    if not success:
        success = click("yinyangliao_12_1_jiyantili")
    if not success:
        success = click("yinyangliao_12_2_jiyangjinbi")
    if success:
        click("yinyangliao_12_3_jinrujiejie")
        click("yinyangliao_12_4_fangshishen")
        click("yinyangliao_12_5_fangshishenjieshu")
        click("yinyangliao_12_6_fanhui")
        click("yinyangliao_12_7_fanhui")
        click("yinyangliao_12_8_fanhui")
        click("yinyangliao_16_fanhui")

    else:
        click("yinyangliao_13_fail_fanhui")
        click("yinyangliao_14_fanhui")
        click("yinyangliao_15_fanhui", 2)
        click("yinyangliao_16_fanhui")

        print(2)
    click("yinyangliao_16_fanhui")


# 点击不好确认的图片
def mutil_click(imgs, sec=0, break_count=15):
    random.shuffle(imgs)
    for img in imgs:
        success = click(img, sec, break_count)
        if success:
            return success
    return False


def switch(account, sub_account):
    t = random.randint(4, 5)
    time.sleep(t)
    for i in ['switch_account_1_1_avater', 'switch_account_1_2_avater', 'switch_account_1_3_avater',
              'switch_account_1_4_avater']:  # 点击头像
        if click(i):
            break
    click("switch_account_2_user_center")  # 用户中心
    click("switch_account_3_switch_account", 2)  # 点击切换账号按钮
    click("switch_account_4_click_wangyi")  # 点击网易图标选择账号
    click(account)  # 选择主账号
    click("switch_account_5_login")  # 登录
    imgs = ["switch_account_6_switch", "switch_account_6_1_switch", "switch_account_6_2_switch"
        , "switch_account_6_3_switch", "switch_account_6_4_switch", "switch_account_6_5_switch"]
    mutil_click(imgs, 0, 25)  # 开始切换
    click("sub_account_" + sub_account)  # 选择子账号名
    click("sub_account_" + sub_account + "_f")  # 选择子账号服务器
    click("switch_account_7_entry", 0, 25)  # 点击进入游戏
    imgs = ["switch_account_7_1_entry", "switch_account_7_2_entry", "switch_account_7_3_entry"
            , "switch_account_7_4_entry"]
    mutil_click(imgs, 0, 25)  # 点击进入游戏
    click("sub_account_" + sub_account + "_avator")  # 点击子账号头像
    t = random.randint(5, 6)
    success = click("switch_account_8_click_screen", t)  # 点击屏幕
    if success:
        print("切换账号成功：" + account + "=>" + sub_account)


def common_task():
    random.shuffle(actions)
    # ['takeGouYu','takeJiaCheng','takeTili','yinyangliao',fengmo]
    for i in actions:
        if i == 'takeGouYu':
            if click(i):
                print("领取勾玉成功")
            else:
                print("！！！！领取勾玉失败！！！！")
        if i == 'takeJiaCheng':
            if click(i):
                click("querenlingqu")
                success = click("tingyuan")
                if success:
                    print("领取加成功")
                else:
                    print("！！！！领取加失败！！！！")
        if i == 'takeTili':
            if click(i):
                print("领取体力成功")
            else:
                print("！！！！领取体力失败！！！！")
        if i == 'email':
            click("email")
            click("email2")
            click("email3")
            click("email4")
            if click("email5"):
                print("领取邮箱成功")
            else:
                print("领取邮箱失败！！！！")
        if i == 'qiandao':
            click("qiandao")
            click("qiandao2")
            if click("qiandao3"):
                print("签到成功")
            else:
                print("签到失败")


def switchAccount(account, sub_account):
    print(sub_account)

    if pyautogui.position()[0] >= pyautogui.size()[0] * 0.98:
        select_mode()

    switch(account, sub_account)

    # yinyangliao()
    return
    # 点击头像
    for i in ['account_1_avater', 'account_2_avater', 'account_3_avater', 'account_4_avater']:
        clicked = False
        if (click(i)):
            clicked = True
            break
    if clicked:
        print("")
    else:
        print("头像失败！！！！")
    # 点击用户中心
    if click("yonghuzhongxin2"):
        print("")
    else:
        print("用户中心失败！！！！")
    # 点击切换账号
    if click("2qiehuanzhanghao"):
        print("")
    else:
        print("切换账号失败！！！！")
    # 开始切换账号
    if click("3wangyi"):
        print("")
    else:
        print("开始切换账号失败！！！！")
    # 切换
    if click(account):
        print("")
    else:
        print(account + "失败！！！！")

    click("5denglu")
    screen = action2.screenshot(monitor)
    click("6qiehuan")
    click("account_" + subAccount)
    click("account_" + subAccount + "_f")
    click("9jinruyouxi")
    click("account_" + subAccount + "_t")
    click("12pingmu")
    t = random.randint(400, 500) / 100
    time.sleep(t)
    random.shuffle(actions)
    # ['takeGouYu','takeJiaCheng','takeTili','yinyangliao',fengmo]
    for i in actions:
        if i == 'takeGouYu':
            if (click(i)):
                print("领取勾玉成功")
        if i == 'takeJiaCheng':
            if (click(i)):
                click("querenlingqu")
                click("tingyuan")
                print("领取加成功")
        if i == 'takeTili':
            if (click(i)):
                print("领取体力成功")
        if i == 'email':
            click("email")
            click("email2")
            click("email3")
            click("email4")
            if (click("email5")):
                print("领取邮箱成功")
            else:
                print("领取邮箱失败！！！！")
        if i == 'qiandao':
            click("qiandao")
            click("qiandao2")
            if (click("qiandao3")):
                print("签到成功")
            else:
                print("签到失败")
        if i == 'yinyangliao':
            click("zhankai")
            click("yinyangliao")
            click("jijie2")
            click("jiejietili")
            click("quchutili")
            click("quchutili2")
            click("lingtilijieshu")
            click("lingtilijieshu")
            click("jijiegouyu")
            click("yucheng")
            click("jiyang")
            click("jiyanggouyu")
            click("jirujiejie")
            click("fangbaidan")
            click("fangbaidanqueren")
            click("fanhui")
            click("fanhui2")
            click("fanhui3")
            click("fanhui4")
            click("fanhui")
            click("fanhui2")
            click("fanhui3")
            click("fanhui4")
        if i == 'fengmo':
            click("tingzhong")
            click("chadiao")
            click("fengmo")
            click("dianjifengmo")
            click("dianjifengmo")
            click("dianjifengmo")
            click("dianjifengmo")
            for i in [1, 2, 3]:
                click("dianjifengmo")
                if i == random.randint(1, 3):
                    break
            click("fengmo5")
            click("fanhui5")
            click("fanhui5")
            click("fanhuitingyuan")
            click("fanhuitingyuan")

            # for i in [1, 2, 3]:
            #     if(click("xunzhaoshouling") == True):
            #         if(click("jijie")):
            #             if(click("jijietiaozhan")):
            #                 t = random.randint(300,400)
            #                 time.sleep(t)
            #                 break
            #             else:
            #                 click("jijiechadiao")


def huanhao():
    for account, values in accounts.items():
        for subAccount in values:
            switchAccount(account, subAccount)


# 探索单人
def huanhao2():
    global last_click
    count = 0
    refresh = 0
    while True:  # 直到取消，或者出错
        if pyautogui.position()[0] >= pyautogui.size()[0] * 0.98:
            select_mode()

        # 截屏
        screen = action2.screenshot(monitor)

        # 设定目标，开始查找
        # 进入后
        click("0touxiang")
        screen = action2.screenshot(monitor)
        click("yonghuzhongxin2")
        screen = action2.screenshot(monitor)
        click("2qiehuanzhanghao")
        screen = action2.screenshot(monitor)
        click("3wangyi")
        screen = action2.screenshot(monitor)
        click("4qiang")
        screen = action2.screenshot(monitor)
        click("5denglu")
        screen = action2.screenshot(monitor)
        click("6qiehuan")
        screen = action2.screenshot(monitor)
        click("7piaoliu")
        screen = action2.screenshot(monitor)
        click("8fuwuqi")
        screen = action2.screenshot(monitor)
        click("9jinruyouxi")
        screen = action2.screenshot(monitor)
        click("10touxiang")
        screen = action2.screenshot(monitor)
        click("12pingmu")
        click("13hunyu")


####################################################
if __name__ == '__main__':
    select_mode()
